package com.hopes.bizz.risk.transfer.service.impl;

import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.transfer.bean.bo.RiskDataBo;
import com.hopes.bizz.risk.transfer.constant.RiskConstant;
import com.hopes.bizz.risk.transfer.constant.RiskRequestType;
import com.hopes.bizz.risk.transfer.dto.RiskTransferData;
import com.hopes.bizz.risk.transfer.bean.entity.RiskTransferAuthEntity;
import com.hopes.bizz.risk.transfer.bean.entity.RiskTransferLogEntity;
import com.hopes.bizz.risk.transfer.handler.RiskDataHandlerFactory;
import com.hopes.bizz.risk.transfer.service.RiskConsumerService;
import com.hopes.bizz.risk.transfer.service.RiskTransferAuthService;
import com.hopes.bizz.risk.transfer.service.RiskTransferLogService;
import com.hopes.bizz.risk.transfer.util.AesGcm256Util;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;


/**
 * @author lyb
 * @version 1.0
 * @description: 风险数据消费处理
 * @date 2024/2/1 20:30
 */

@Component
@Slf4j
@AllArgsConstructor
public class RiskConsumerServiceImpl implements RiskConsumerService {

    private RiskTransferAuthService riskTransferAuthService;

    private RiskTransferLogService riskTransferLogService;

    private RiskDataHandlerFactory riskDataHandlerFactory;

    @Override
    public void handler(RiskTransferData riskTransferData) {
        String data = riskTransferData.getData();
        String batchId = riskTransferData.getBatchId();
        log.info("开始处理风险数据,批次id：{},数据:{}", batchId, data);
        if (StringUtils.isEmpty(data)) {
            log.info("风险数据为空,批次id：{}", batchId);
            return;
        }

        // 读取批次信息
        RiskTransferLogEntity riskTransferLog = riskTransferLogService.lambdaQuery()
                .eq(RiskTransferLogEntity::getStatus, RiskConstant.RISK_TRANSFER_HANDLER_STATUS_PROCESSING)
                .eq(RiskTransferLogEntity::getBatchId, batchId)
                .one();

        if (Objects.isNull(riskTransferLog)) {
            log.info("该批次信息为空");
            return;
        }

        // 读取企业认证信息
        RiskTransferAuthEntity riskTransferAuth = riskTransferAuthService.lambdaQuery()
                .eq(RiskTransferAuthEntity::getOrgId, riskTransferLog.getOrgId())
                .eq(RiskTransferAuthEntity::getUse, Boolean.TRUE)
                .one();

        if (Objects.isNull(riskTransferAuth)) {
            log.info("企业认证信息未开通权限");
            return;
        }

        // 解密
        String key = riskTransferAuth.getKey();
        String[] keyArr = key.split(",");
        String decryptData = AesGcm256Util.decrypt(data,
                AesGcm256Util.hexToByte(keyArr[0]),
                AesGcm256Util.hexToByte(keyArr[1]));

        // 开始处理
        RiskDataBo riskDataBo = new RiskDataBo();
        riskDataBo.setData(decryptData);
        riskDataBo.setRiskTransferLog(riskTransferLog);

        try {
            riskDataHandlerFactory.getHandlerByRequestType(RiskRequestType.getRiskRequestTypeByCode(riskTransferLog.getRequestType())).handler(riskDataBo);
        } catch (Exception e) {
            log.error("风险数据处理失败：{}", e.getMessage());
        }
    }
}
